import CommandsPlugin from './command';
import QueryPlugin from './query';
import DNDPlugin from './DNDPlugin';
import SchemaPlugin from './schema';
import commands from '../command';
import queries from '../query';
import SCHME_ERROR from '../constant/schema-error';

const schema = {
  data: {
    name: name => {
      return !!name;
    },
    column: column => {
      return !!column;
    },
  },
  normalize: (error, node) => {
    let { data } = node;
    switch (error.code) {
      case SCHME_ERROR.DATA_INVALID:
        switch (error.key) {
          case 'name':
            data = data.set('name', `${node.type}-${node.key}`);
            return node.set('data', data);
          case 'column':
            data = data.set('column', `${node.type}-${node.key}`);
            return node.set('data', data);
          default:
            return node;
        }
      default:
        return node;
    }
  },
};

export default function CorePlugin({ plugins = [] }) {
  return [
    CommandsPlugin(commands),
    QueryPlugin(queries),
    DNDPlugin(),
    ...plugins,
    SchemaPlugin({ schema }),
  ];
}
